<script setup name="Completed">
import { pcaTextArr } from 'element-china-area-data'
import customerInformationService from '@/api/customerInformation/index.js'
import { isObject } from '@/utils/validate.js'
import ContractDraw from '@/views/system/contract/components/ContractDraw.vue'
import contractService from '@/api/contract/index.js'
import Toolbar from '@/components/Toolbar/Toolbar.vue'
import SearchBar from '@/components/SearchBar/SearchBar.vue'
import { useDict } from '@/utils/dict.js'

const { proxy } = getCurrentInstance()

const {
  sys_contract_type,
  sys_contractaame,
  sys_contract_status,
  sys_case_type,
  sys_product_name,
  sys_patent_status,
} = useDict('sys_contract_type', 'sys_contractaame', 'sys_contract_status', 'sys_case_type', 'sys_product_name', 'sys_patent_status')

const isBigContract = ref('0')
const tableLoading = ref(false)
const dialogVisible = ref(false)

const customerQueryList = ref([])
const queryParams = ref({})
const locationArea = ref([])

const searchConfig = ref([
  {
    label: '合同编号',
    field: 'contractNo',
    type: 'input',
    defaultShow: true,
  },
  {
    label: '合同类别',
    field: 'contractType',
    type: 'select',
    options: {
      data: sys_contract_type,
    },
    defaultShow: true,
  },
  {
    label: '客户所在区域',
    field: 'locationArea',
    type: 'custom',
    defaultShow: true,
  },
  {
    label: '客户名称',
    field: 'customerId',
    type: 'select',
    defaultShow: true,
    options: {
      data: customerQueryList,
      label: 'customerName',
      value: 'customerId',
    },
  },
  {
    label: '递交端口',
    field: 'deliveryWindow',
    type: 'input',
  },
  {
    label: '合同开始时间',
    field: 'contractStartTime',
    type: 'daterange',
  },
  {
    label: '业务经理',
    field: 'nickName',
    type: 'input',
  },
  {
    label: '备注',
    field: 'remark',
    type: 'input',
  },
])

// 表格数据
const tableData = ref([])
const tableColumns = ref([
  {
    type: 'checkbox',
    width: 50,
    align: 'center',
  },
  {
    type: 'seq',
    width: 60,
    align: 'center',
  },
  {
    title: '合同编号',
    field: 'contractNo',
    align: 'center',
    minWidth: 100,
    showOverflow: 'tooltip',
  },
  {
    title: '合同类别',
    field: 'contractType',
    align: 'center',
    minWidth: 50,
    slots: {
      default: 'contractType',
    },
  },
  {
    title: '客户名称',
    field: 'customerName',
    align: 'center',
    minWidth: 90,
  },
  {
    title: '客户所在区域',
    field: 'locationArea',
    align: 'center',
    minWidth: 120,
  },
  {
    title: '递交端口',
    field: 'submitcompanyName',
    align: 'center',
    minWidth: 120,
  },
  {
    title: '合同开始时间',
    field: 'contractStartTime',
    align: 'center',
    minWidth: 80,
    formatter: ({ cellValue }) => {
      return cellValue ? cellValue.split(' ')[0] : ''
    },
  },
  { title: '业务经理', field: 'nickName', align: 'center', minWidth: 50 },
  {
    title: '状态', field: 'auditStatus', align: 'center',
    slots: { default: 'auditStatus' },
    minWidth: 50,
  },
  {
    title: '审核意见',
    field: 'auditSuggest',
    align: 'center',
    minWidth: 80,
  },
  { title: '备注', field: 'remark', align: 'center', minWidth: 60, showOverflow: 'tooltip' },
  {
    title: '操作',
    field: 'action',
    align: 'center',
    minWidth: 250,
    fixed: 'right',
    slots: {
      default: 'action',
    },
  },
])

const ids = ref([])
const pagerConfig = ref({
  total: 0,
  currentPage: 1,
  pageSize: 10,
})
const contractId = ref(null)
const isView = ref(false)
const affiliatedBusinessesVisible = ref(false)
const affiliatedBusinessesData = ref([])
const affiliatedBusinessesColumns = ref([
  { title: '案件编号', field: 'reelNumber', align: 'center' },
  { title: '业务名称', field: 'patentName', align: 'center' },
  { title: '业务类型', field: 'businessName', align: 'center' },
  { title: '状态', field: 'status', align: 'center', slots: {
    default: 'status',
  } },
  { title: '专利申请号', field: 'applicationNumber', align: 'center' },
  { title: '专利号', field: 'certificateNumber', align: 'center' },
],
)
const affiliatedBusinessesPagerConfig = ref({
  total: 0,
  currentPage: 1,
  pageSize: 10,
})

onMounted(() => {
  getList()
  getQueryList()
})

function getQueryList() {
  customerInformationService.getQueryList().then((res) => {
    customerQueryList.value = res.data
  })
}

function getList() {
  tableLoading.value = true
  const params = {
    ...queryParams.value,
    auditStatus: '3',
    pageSize: pagerConfig.value.pageSize,
    pageNum: pagerConfig.value.currentPage,
  }
  contractService.list(params).then((res) => {
    tableData.value = res.rows
    pagerConfig.value.total = res.total
    tableLoading.value = false
  })
}

function handleSearch(e, type) {
  if (type === 'reset') {
    locationArea.value = []
  }
  queryParams.value = {
    ...e,
    locationArea: locationArea.value ? locationArea.value.join('/') : null,
    contractStartTimeMin: e.contractStartTime ? e.contractStartTime[0] : null,
    contractStartTimeMax: e.contractStartTime ? e.contractStartTime[1] : null,
  }
  delete queryParams.value.contractStartTime
  pagerConfig.value.currentPage = 1
  pagerConfig.value.pageSize = 10
  getList()
}

function handleExport() {
  proxy.download('system/contract/export', { ...queryParams.value, exportIds: ids.value.join(',') }, `contract__${new Date().getTime()}.xlsx`)
}

function handlePageChange({ currentPage, pageSize }) {
  pagerConfig.value.currentPage = currentPage
  pagerConfig.value.pageSize = pageSize
  getList()
}

function handleDialogVisible(val) {
  if (isObject(val)) {
    dialogVisible.value = false
    queryParams.value = {}
    pagerConfig.value.currentPage = 1
    pagerConfig.value.pageSize = 10
    getList()
  }
}

function handleView(id) {
  contractId.value = id
  isView.value = true
  dialogVisible.value = true
}

function handleAffiliatedBusinesses(id) {
  contractId.value = id
  getByContractList()
}

function getByContractList() {
  const params = {
    pageSize: affiliatedBusinessesPagerConfig.value.pageSize,
    pageNum: affiliatedBusinessesPagerConfig.value.currentPage,
  }
  contractService.getZlByContractList(contractId.value, params).then((res) => {
    affiliatedBusinessesData.value = res.rows
    affiliatedBusinessesPagerConfig.value.total = res.total
    affiliatedBusinessesVisible.value = true
  })
}

function handleAffiliatedBusinessesPageChange({ currentPage, pageSize }) {
  affiliatedBusinessesPagerConfig.value.currentPage = currentPage
  affiliatedBusinessesPagerConfig.value.pageSize = pageSize
  getByContractList()
}
</script>

<template>
  <div class="app-container">
    <el-card shadow="never">
      <SearchBar :search-config="searchConfig" @search="handleSearch">
        <template #locationArea>
          <el-cascader
            v-model="locationArea"
            :options="pcaTextArr"
            size="default"
            placeholder="请选择所属区域"
            clearable
            class="w-full"
          />
        </template>
      </SearchBar>
    </el-card>
    <el-card shadow="never" class="mt-5">
      <Toolbar>
        <el-button
          v-hasPermi="['system:contract:export']"
          type="warning"
          class="el-gradient-warning"
          @click="handleExport()"
        >
          导出
          <el-icon class="el-icon--right">
            <FolderAdd />
          </el-icon>
        </el-button>
      </Toolbar>
      <vxe-grid
        :row-config="{ keyField: 'contractId' }"
        :data="tableData"
        :columns="tableColumns"
        :loading="tableLoading"
        :pager-config="pagerConfig"
        header-cell-class-name="headerCellClass"
        @page-change="handlePageChange"
        max-height="500px"
      >
        <template #contractType="{ row }">
          <dict-tag :options="sys_contract_type" :value="row.contractType" />
        </template>
        <template #auditStatus="{ row }">
          <dict-tag :options="sys_contract_status" :value="row.auditStatus" />
        </template>
        <template #action="{ row }">
          <el-button
            v-hasPermi="['system:contract:getZlByContract']"
            type="primary"
            size="small"
            text
            @click="handleAffiliatedBusinesses(row.contractId)"
          >
            关联业务信息
          </el-button>
          <el-button
            size="small"
            text
            type="info"
            @click="handleView(row.contractId)"
          >
            查看
          </el-button>
        </template>
      </vxe-grid>
    </el-card>
    <ContractDraw
      v-model:visible="dialogVisible"
      :contract-id="contractId"
      :form-disabled="isView"
      @update:visible="handleDialogVisible"
    />
    <el-dialog
      v-model="affiliatedBusinessesVisible"
      width="60%"
      class="custom-dialog"
      :append-to-body="true"
      :destroy-on-close="true"
    >
      <template #header>
        <div class="dialog-header">
          <span class="relative top-3 text-[16px]">
            关联业务信息
          </span>
        </div>
      </template>
      <vxe-grid
        :data="affiliatedBusinessesData"
        :columns="affiliatedBusinessesColumns"
        header-cell-class-name="headerCellClass"
        :pager-config="affiliatedBusinessesPagerConfig"
        @page-change="handleAffiliatedBusinessesPageChange"
      >
        <template #status="{ row }">
          <dict-tag :options="sys_patent_status" :value="row.status" />
        </template>
      </vxe-grid>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
</style>
